<template>
  <div class="main-view">
    <!-- 左右布局 -->
    <div class="pr-2" style="border-right: 1px solid #ccc">
      <div>
        <div @click="toPage('/admin/main/welcome')" class="fs-3">首页</div>
        <!-- 折叠菜单 -->
        <div class="mt-5">
          <NCollapse arrow-placement="right">
            <NCollapseItem title="系统设置" class="div">
              <div @click="toPage('/admin/main/log')">日志</div>
              <div @click="toPage('/admin/main/report')">举报管理</div>
              <div @click="alogout">安全退出</div>
            </NCollapseItem>
          </NCollapse>
        </div>
      </div>
    </div>

    <!-- 右边是上下布局 -->
    <div>
      <div class="fs-3 tc" style="border-bottom: 1px solid #ccc">后台管理简易版</div>
      <div class="p-3">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { NCollapse, NCollapseItem, useDialog, useMessage } from 'naive-ui'
const message = useMessage()
const dialog = useDialog()
import { useRouter } from 'vue-router'
import { Api } from '@/types/Api'
import type { BaseResult } from '@/types/BaseResult'
const router = useRouter()

const toPage = (url: string) => {
  router.push(url)
}

// 安全退出
const loading = ref(false)

const alogout = () => {
  dialog.error({
    title: '警告',
    content: `确定退出吗？`,
    positiveText: '确定',
    negativeText: '取消',
    onPositiveClick: () => {
      loading.value = true
      Api.delete('/admin/auth', {}, (data: BaseResult) => {
        loading.value = false
        if (data.success) {
          message.success('退出成功')
          router.push('/')
          return
        }
        message.error(data.message)
      })
    },
  })
}
</script>

<style scoped lang="scss">
.main-view {
  display: flex;
  height: 100vh;
  // 左边固定宽度
  > div:first-of-type {
    width: 10rem;
    padding: 1rem;
  }
  // 右边宽度自适应
  > div:last-of-type {
    flex: 1;
    display: flex;
    flex-direction: column;
    > div:last-of-type {
      flex: 1;
      overflow-y: auto;
    }
    > div:first-of-type {
      padding: 1rem;
    }
  }
}

.div {
  div {
    cursor: pointer;
    padding-left: 1rem;
    border-left: 1px solid #ccc;
    margin-bottom: 0.5rem;
  }
}
</style>
